﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using Sunboxsoft.Common;
using Sunboxsoft.ASMS.Client.UI.Properties;
using Calculation = Sunboxsoft.ASMS.Client.Domain.Calculation;
namespace Sunboxsoft.ASMS.Client.UI.Settling
{
    /// <summary>
    /// 服务单收银详细信息
    /// </summary>
    public partial class SettlementBillDetailView : UserControl, IView
    {
        /// <summary>
        /// 维修单号
        /// </summary>
        private Guid _maintenanceBillId;
        /// <summary>
        /// 维修单详细信息
        /// </summary>
        WebRef.MaintenanceBillDetail billDetail = null;

        Promise _promise = new Promise();

        public SettlementBillDetailView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 根据维修单Id获取收银单信息
        /// </summary>
        /// <param name="maintenanceBillId">维修单Id</param>
        public SettlementBillDetailView(Guid maintenanceBillId)
        {
            InitializeComponent();
            this._maintenanceBillId = maintenanceBillId;
        }

        #region IView 成员
        public void RegisterAction(ActionNodeCollection collection)
        {
            collection.AddAction(new ActionNode("back", Resources.isBack, "返回", btnBack_Click))
               .AddAction(new ActionNode("carOut", Resources.isBus, "车辆出厂", btnCarOutClick) { IsShow = false })
               .AddAction(new ActionNode("refresh", Resources.isRefresh, "刷新", btnRefresh_Click));
        }
        public void Resume(ViewResumeArgs args)
        {
            if (args.ClosedArgs.Result)
            {

            }
        }
        public void Active(ViewActiveArgs args)
        {
            _promise.Then(BindSettleMentBillData).Run();
        }
        public ViewHeader GetHeader()
        {
            return new ViewHeader("服务单收银详细信息");
        }
        #endregion

        #region Method
        private void IninCheckedInfo(WebRef.VSettlementBill bill, List<WebRef.VPayRecord> list)
        {
            var control = new Components.ShowCheckedOutView(bill, list);
            control.Dock = DockStyle.Top;
            this.baseCashPanel.Controls.Add(control);
        }
        /// <summary>
        /// 绑定结算单信息
        /// </summary>
        private void BindSettleMentBillData()
        {
            UIContext.Current.InvokeAsync(() =>
            {
                SingleResult<WebRef.SettlementBillDetail> result = new Service.GetService().GetSettlementBillDetailByMaintennanceBillId(_maintenanceBillId);
                this.BeginInvoke(new Action(() =>
                {
                    if (result.IsSuccess)
                    {
                        this.TabPageServiceProject.Text = string.Format("服务项目({0}项)", result.Data.MaintenanceBill.ServiceItemCount.ToString());
                        this.TabPageGoods.Text = string.Format("商品&配件({0}项)", result.Data.MaintenanceBill.MaterialCount.ToString());
                        //订单基本信息
                        this.lblInfo.Text = string.Format("订单编号：{0}-创建时间：{1}", result.Data.MaintenanceBill.Number, result.Data.MaintenanceBill.CreatedTime);

                        this.txtBookingBillId.Text = result.Data.MaintenanceBill.BookingBillNumber;
                        this.txtSalesManagerName.Text = result.Data.MaintenanceBill.SalesManagerName;
                        if (result.Data.MaintenanceBill.EstimatedDeliveryDate != null)
                        {
                            this.txtEstimatedDeliveryDate.Text = result.Data.MaintenanceBill.EstimatedDeliveryDate.Value.ToString("yyyy/MM/dd");
                        }
                        this.txtCustomerName.Text = result.Data.MaintenanceBill.CustomerName;
                        this.txtCustomerTelephone.Text = result.Data.MaintenanceBill.CustomerTelephone;
                        this.txtLPN.Text = result.Data.MaintenanceBill.LPN;
                        this.txtVIN.Text = result.Data.MaintenanceBill.VIN;
                        this.txtEIN.Text = result.Data.MaintenanceBill.EIN;
                        this.txtOilMass.Text = result.Data.MaintenanceBill.OilMass.ToString();
                        this.txtRunningKilometers.Text = result.Data.MaintenanceBill.RunningKilometers.ToString();
                        this.txtMemo.Text = result.Data.MaintenanceBill.Memo;
                        this.txtAmount.Text = result.Data.MaintenanceBill.Amount.ToString();
                        this.GCServiceProject.DataSource = result.Data.ServiceItemList;
                        this.GCGoodsData.DataSource = result.Data.MaterialList;
                        this.GCOther.DataSource = result.Data.PayItemList;
                        this.GCPackage.DataSource = result.Data.PackageItemList;

                        string strOutMess = "";
                        decimal total = Calculation.SettlementBillCalculation.Calc(
                            result.Data.ServiceItemList == null ? null : result.Data.ServiceItemList.ToArray(),
                            result.Data.MaterialList == null ? null : result.Data.MaterialList.ToArray(),
                            result.Data.PayItemList == null ? null : result.Data.PayItemList.ToArray(),
                            result.Data.PackageItemList == null ? null : result.Data.PackageItemList.ToArray(),
                            null,
                            0,
                            true,
                            out strOutMess);

                        this.ctllblSettlementBillTextInfo.Text = strOutMess;
                        this.ctllblSettlementBillTotal.Text = string.Format("应收金额：{0}", Math.Round(total, 2).ToString());

                        IninCheckedInfo(result.Data.Bill, result.Data.PayRecordList.ToList());
                        if (result.Data.MaintenanceBill.Status == Domain.Constant.MaintenanceBillStatus.WaitDelivery)
                        {
                            this.ShowAction("carOut", true);
                        }
                        else
                        {
                            this.ShowAction("carOut", false);
                        }
                        _promise.Resolve();
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError(result.Message);
                        _promise.Reject();
                    }
                }));
            });
        }
        #endregion

        #region Action
        /// <summary>
        /// 返回
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBack_Click(object sender, object e)
        {
            UIContext.Current.ViewManager.CloseView(true, "Detail");
        }
        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRefresh_Click(object sender, object e)
        {
            BindSettleMentBillData();
        }
        /// <summary>
        /// 车辆出厂
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCarOutClick(object sender, object e)
        {
            if (UIContext.Current.ViewManager.ShowQuestion("确定要出厂吗？"))
            {
                UIContext.Current.InvokeAsync(() =>
                {
                    ServiceResult result = new Service.DeliverService().DeliverMaintenanceBill(_maintenanceBillId);
                    this.BeginInvoke(new Action(() =>
                    {
                        if (result.IsSuccess)
                        {
                            UIContext.Current.ViewManager.CloseView(true, null);
                            _promise.Resolve();

                        }
                        else
                        {
                            UIContext.Current.ViewManager.ShowError(result.Message);
                            _promise.Reject();
                        }
                    }));
                });
            }
        }
        #endregion
    }
}
